//
//  ImageCell.m
//  quickBatchManager
//
//  Created by jack on 15/9/16.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "ImageCell.h"

@implementation ImageCell
{
    ProductModel *product;
}
@synthesize contentView;
- (void)awakeFromNib {
    contentView.layer.borderWidth = 1;
    contentView.layer.borderColor = [UIColor grayColor].CGColor;
   
    _deleteBtn.userInteractionEnabled = YES;
    [_deleteBtn bk_whenTapped:^{
        [self onDel];
    }];
    
    
    _editBtn.userInteractionEnabled = YES;
    [_editBtn bk_whenTapped:^{
        [self onEdit];
    }];
    
    
    UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
    longpressGesutre.delegate = self;
    //长按时间为1秒
    longpressGesutre.minimumPressDuration=0.5;
    //允许15秒中运动
    longpressGesutre.allowableMovement=0;
    //所需触摸1次
    longpressGesutre.numberOfTouchesRequired=1;
    [contentView addGestureRecognizer:longpressGesutre];
    
 

}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"ImageCell" owner:self options:nil];
        
        // 如果路径不存在，return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类，return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
        self.contentView.layer.borderWidth = 3;
        self.contentView.layer.borderColor = [[UIColor colorWithRed:45/255.0 green:45/255.0 blue:45/255.0 alpha:0.5] CGColor];
    }
    
    
    return self;
}


-(void)setData:(id)data
{
    mData = data;
    
    if([data isKindOfClass:[AddProductModel class]])
    {
        AddProductModel *model=(AddProductModel*)data;
        product=model.product;
       
        _editView.hidden = YES;
       // imageCell.parentVC=self;
        
    }
    else if([data isKindOfClass:[SaleOrderInfoModel class]])
    {
        SaleOrderInfoModel *model=(SaleOrderInfoModel*)data;
        product=model.product;
        
        _editView.hidden = YES;
    }
    else if([data isKindOfClass:[ProductModel class]])
    {
        product=(ProductModel*)data;
    }
    
    
    _productNumberLabel.text =  [NSString stringWithFormat:@"  %@",product.modelNo];
    _priceLabel.text = [NSString stringWithFormat:@"￥ %0.1f",product.salePrice];
    if([product.photo isValid])
    {
        NSString *imagePath = [ToolUtils getImageLocalPath:product.photo];
        [_productImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]]];
    }
    
}


-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:_mSuperView];
    CGPoint center;
    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;
    switch (state) {
        case UIGestureRecognizerStateBegan:
            _dragView = [self customSnapshoFromView:self];
            [_dragView setFrame:CGRectMake(location.x - self.frame.size.width/2 , location.y - self.frame.size.height/2, self.frame.size.width, self.frame.size.height)];
            [_mSuperView addSubview:_dragView];
           
            break;
        
        case UIGestureRecognizerStateChanged:
            center = _dragView.center;
            center.y = location.y;
            center.x = location.x;
            _dragView.center = center;
            
            if(CGRectContainsPoint(_goodView.frame, location))
            {
                     _goodView.layer.borderWidth = 2;
                    _goodView.layer.borderColor = [[UIColor yellowColor] CGColor];
            }else
            {
                    _goodView.layer.borderWidth = 0;
            }
            break;
        
        default:
            [UIView animateWithDuration:0.25 animations:^{
                _dragView.transform = CGAffineTransformIdentity;
                _dragView.alpha = 0.0;
                
            } completion:^(BOOL finished) {
                [_dragView removeFromSuperview];
                _dragView = nil;
                _goodView.layer.borderWidth = 0;
                
                if(CGRectContainsPoint(_goodView.frame, location))
                {
                    NSLog(@"添加。。。");
                   if([_delegate respondsToSelector:@selector(imageDragToGoodView:addProductModel:)])
                   {
                       [_delegate imageDragToGoodView:self addProductModel:mData];
                   }
                }
            }];
            break;
    }
    
}


- (UIView *)customSnapshoFromView:(UIView *)inputView {
    
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    
    return snapshot;
}

-(void)onEdit
{
    EditProductViewController *vc=[[EditProductViewController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    vc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
    _parentVC.definesPresentationContext = YES;
    vc.product=product;
    [_parentVC presentViewController:vc animated:YES completion:nil];
}

-(void)onDel
{
    UIAlertView *alert=[UIAlertView bk_alertViewWithTitle:@"提示" message:@"是否删除产品"];
    [alert bk_setCancelButtonWithTitle:@"取消" handler:nil];
    [alert bk_setCancelButtonWithTitle:@"确定" handler:^{
        [AppService delProductById:product.productId];
        [_parentVC reloadData:ChangeTypeDefult];
    }];
    [alert show];
    
    
    
}

@end
